//
//  ToolMethod.m
//  XinDong
//
//  Created by tt on 12-11-12.
//  Copyright (c) 2012年 HengYongKeJi. All rights reserved.
//

#import "ToolMethod.h"
#include <sys/types.h>
#include <sys/sysctl.h>

@implementation ToolMethod

+ (void)showAlert:(NSString*)message delegate:(id)delegate{
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
													message:message
												   delegate:delegate
										  cancelButtonTitle:LocalizedString(@"Ok")
										  otherButtonTitles:nil];
    alert.tag = 0;
	[alert show];
}

+(UIImage*) formatUploadImage:(UIImage*)photoimage
{
    int kMaxResolution = 960;
	
	CGImageRef imgRef = photoimage.CGImage;
    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);
	
	NSData *imgData = UIImageJPEGRepresentation(photoimage, 0.9);
    
	CGAffineTransform transform = CGAffineTransformIdentity;
    CGRect bounds = CGRectMake(0, 0, width, height);
	
	if ([imgData length] > 524288) {
		if (width > kMaxResolution || height > kMaxResolution)
		{
			CGFloat ratio = width/height;
			if (ratio > 1)
			{
				bounds.size.width = kMaxResolution;
				bounds.size.height = bounds.size.width / ratio;
			}
			else
			{
				bounds.size.height = kMaxResolution;
				bounds.size.width = bounds.size.height * ratio;
			}
		}
	} else {
		bounds.size.width = width;
		bounds.size.height = height;
	}
	
	
    CGFloat scaleRatio = bounds.size.width / width;
    CGFloat scaleRatioheight = bounds.size.height / height;
    CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
    CGFloat boundHeight;
    UIImageOrientation orient =photoimage.imageOrientation;
    switch(orient)
    {
			
        case UIImageOrientationUp: //EXIF = 1
            transform = CGAffineTransformIdentity;
            break;
			
        case UIImageOrientationUpMirrored: //EXIF = 2
            transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            break;
			
        case UIImageOrientationDown: //EXIF = 3
            transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;
			
        case UIImageOrientationDownMirrored: //EXIF = 4
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
            transform = CGAffineTransformScale(transform, 1.0, -1.0);
            break;
			
        case UIImageOrientationLeftMirrored: //EXIF = 5
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;
			
        case UIImageOrientationLeft: //EXIF = 6
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;
			
        case UIImageOrientationRightMirrored: //EXIF = 7
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeScale(-1.0, 1.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;
			
        case UIImageOrientationRight: //EXIF = 8
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;
			
        default:
            //[NSException raise:NSInternalInconsistencyException format:@"Invalid?image?orientation"];
            break;
    }
	
    UIGraphicsBeginImageContext(bounds.size);
	
    CGContextRef context = UIGraphicsGetCurrentContext();
	
    if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft)
    {
        CGContextScaleCTM(context, -scaleRatio, scaleRatioheight);
        CGContextTranslateCTM(context, -height, 0);
    }
    else
    {
        CGContextScaleCTM(context, scaleRatio, -scaleRatioheight);
        CGContextTranslateCTM(context, 0, -height);
    }
	
    CGContextConcatCTM(context, transform);
	
    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return imageCopy;
}

+ (NSString *)distanceFromMetoX:(NSString *)xPoint toY:(NSString *)yPoint
{
//    CLLocation *peopleLoc = [[CLLocation alloc] initWithLatitude:[yPoint doubleValue] longitude:[xPoint doubleValue]];
//    double distance = [[AppDelegate appDelegate].currentLocation distanceFromLocation:peopleLoc];
    NSString *distanceString=@"";
//    if(distance <1000){
//        distanceString = [NSString stringWithFormat:@"%0.2f m",distance];
//    }else{
//        distanceString = [NSString stringWithFormat:@"%0.2f km",distance/1000];
//    }
    return distanceString;
}

+ (NSString *)getAgeWith:(int)birthday
{
    NSString *stringNow = [NSString stringWithFormat:@"%.4f",[[NSDate date] timeIntervalSince1970]];
    int nowTe = [stringNow intValue];
    NSDateFormatter *formatterNow =[[NSDateFormatter alloc] init];
    NSDate *beginNow = [NSDate dateWithTimeIntervalSince1970:nowTe];
    NSDate *dateNow = beginNow;
    [formatterNow setDateFormat:@"yyyy"];
    NSString *stNow = [formatterNow stringFromDate:dateNow];
    
    NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
    NSDate *begin = [NSDate dateWithTimeIntervalSince1970:birthday];
    NSDate *date = begin;
    [formatter setDateFormat:@"yyyy"];
    NSString *st = [formatter stringFromDate:date];
    
    NSString *age = [NSString stringWithFormat:@"%i",[stNow intValue] - [st intValue]];
    
    return age;
}

+ (NSString*)escapeTwitterTime:(NSString*)timestamp{
	NSString *timeStr = @"";
	NSTimeZone *tzGMT = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [NSTimeZone setDefaultTimeZone:tzGMT];
	NSDate *now = [NSDate date];
	NSDate *that = [NSDate dateWithTimeIntervalSince1970: [timestamp intValue]];
	
	//NSTimeInterval nowtimestamp = [now timeIntervalSince1970];
	//NSTimeInterval thatDaytimestamp = [that timeIntervalSince1970];
	
	NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setTimeZone:tzGMT];
	
	[outputFormatter setDateFormat: @"yyyy"];
	int nowYear = [[outputFormatter stringFromDate:now] intValue];
	int thatYear = [[outputFormatter stringFromDate:that] intValue];
	
	[outputFormatter setDateFormat: @"M"];
	int nowMonth = [[outputFormatter stringFromDate:now] intValue];
	int thatMonth = [[outputFormatter stringFromDate:that] intValue];
	
	[outputFormatter setDateFormat: @"d"];
	int nowDay = [[outputFormatter stringFromDate:now] intValue];
	int thatDay = [[outputFormatter stringFromDate:that] intValue];
	
	NSTimeInterval disTimeInterval = [now timeIntervalSinceDate:that];
	int dis = disTimeInterval;
	if (dis < 0) {
		timeStr = @"刚刚";
	}else if (nowMonth == thatMonth && nowYear == thatYear){
		if (nowDay == thatDay) {
			if (dis < 3600) {
				if (dis/60 == 0) {
					timeStr = @"刚刚";
				} else {
					timeStr = [NSString stringWithFormat:@"%d分钟前",dis/60];
				}
			} else {
				[outputFormatter setDateFormat: @"今天 H:mm"];
				timeStr = [outputFormatter stringFromDate:that];
			}
		} else if(nowDay == thatDay + 1){
			[outputFormatter setDateFormat: @"昨天 H:mm"];
			timeStr = [outputFormatter stringFromDate:that];
		} else if(nowDay == thatDay + 2){
			[outputFormatter setDateFormat: @"前天 H:mm"];
			timeStr = [outputFormatter stringFromDate:that];
		} else {
			[outputFormatter setDateFormat: @"M月d日 H:mm"];
			timeStr = [outputFormatter stringFromDate:that];
		}
	}else if (nowYear == thatYear && nowMonth != thatMonth){
		[outputFormatter setDateFormat: @"M月d日 H:mm"];
		timeStr = [outputFormatter stringFromDate:that];
	}else{
		[outputFormatter setDateFormat: @"yyyy年 M月d日"];
		timeStr = [outputFormatter stringFromDate:that];
	}
	
	return timeStr;
}

+ (NSString *)escapeActivityTime:(NSString*)timestamp
{
    NSString *timeStr = @"";
	NSTimeZone *tzGMT = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [NSTimeZone setDefaultTimeZone:tzGMT];
	NSDate *that = [NSDate dateWithTimeIntervalSince1970: [timestamp intValue]];
	
	//NSTimeInterval nowtimestamp = [now timeIntervalSince1970];
	//NSTimeInterval thatDaytimestamp = [that timeIntervalSince1970];
	
	NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setTimeZone:tzGMT];
	
    [outputFormatter setDateFormat: @"yyyy-M-d"];
    timeStr = [outputFormatter stringFromDate:that];
	
	return timeStr;
}

#pragma mark - 获得机型
+ (NSString*)getMachine{
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *name = malloc(size);
    sysctlbyname("hw.machine", name, &size, NULL, 0);
    
    NSString *machine = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];
    
    free(name);
    
    if( [machine isEqualToString:@"i386"] || [machine isEqualToString:@"x86_64"] ) machine = @"ios_Simulator";
    else if([@"iPhone1,1" isEqualToString:machine] ) machine = @"iPhone_1G";
    else if([@"iPhone1,2" isEqualToString:machine] ) machine = @"iPhone_3G";
    else if([@"iPhone2,1" isEqualToString:machine] ) machine = @"iPhone_3GS";
    else if([@"iPhone3,1" isEqualToString:machine] ) machine = @"iPhone_4";
    else if([@"iPhone4,1" isEqualToString:machine] ) machine = @"iPhone_4S";
    else if([@"iPhone5,2" isEqualToString:machine] ) machine = @"iPhone_5";
    else if([@"iPod1,1" isEqualToString:machine]) machine = @"iPod_Touch_1G";
    else if([@"iPod2,1" isEqualToString:machine]) machine = @"iPod_Touch_2G";
    else if([@"iPod3,1" isEqualToString:machine]) machine = @"iPod_Touch_3G";
    else if([@"iPod4,1" isEqualToString:machine]) machine = @"iPod_Touch_4G";
    else if([@"iPad1,1" isEqualToString:machine]) machine = @"iPad1";
    else if([@"iPad2,1" isEqualToString:machine]) machine = @"iPad_2";
    else if([@"iPad3,1" isEqualToString:machine]) machine = @"iPad_3";
    else machine = @"保密";
    
    return machine;
}
@end
